﻿
namespace SistemaLogistica.Business.Entities.Generics
{
    using System;
    using System.Collections.Generic;

    public sealed class GenericComparer<T> : IComparer<T>
    {
        public enum SortOrder
        {
            Ascending,
            Descending
        };

        private string sortColumn;
        private SortOrder sortingOrder;

        public GenericComparer(string sortColumn, SortOrder sortingOrder)
        {
            this.sortColumn = sortColumn;
            this.sortingOrder = sortingOrder;
        }
       
        public string SortColumn
        {
            get { return sortColumn; }
        }

        public SortOrder SortingOrder
        {
            get { return sortingOrder; }
        }

        public int Compare(T x, T y)
        {
            var propertyInfo = typeof(T).GetProperty(sortColumn);
            var obj1 = (IComparable)propertyInfo.GetValue(x, null);
            var obj2 = (IComparable)propertyInfo.GetValue(y, null);
            if (sortingOrder == SortOrder.Ascending)
            {
                return (obj1.CompareTo(obj2));
            }
            else
            {
                return (obj2.CompareTo(obj1));
            }
        }
    }
}
